/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lorry.storage;

import org.apache.lorry.runtime.WorkerConfig;
import org.apache.lorry.util.Callback;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;

/**
 * <p>
 * 提供了一个接口OffsetBackingStore用于存储每个Connector当前订阅源实例的位点信息offset（例如MySql的binlog），offset在KafkaConnect中是一个key-value的值，
 * 具体由每个Connector自己去定义，OffsetBackingStore只会支持读写字节类型操作（实际上是ByteBuffer）,对于ByteBuffer的序列化和反序列需要Connector自己实现。
 * </p>
 *
 * <p>
 * 从上面的接口参数可以看出，OffsetBackingStore需要支持批量读写key-value操作，是考虑到减少网络请求次数。
 * 由于OffsetBackingStore是一个共享资源，可能由许多与单个任务相关联的OffsetStorage实例使用，
 * 因此调用者必须确保键包含关于连接器的信息，以便共享名称空间不会导致键冲突。
 * </p>
 */
public interface OffsetBackingStore {

    /**
     * Start this offset store.
     */
    void start();

    /**
     * Stop the backing store. Implementations should attempt to shutdown gracefully, but not block
     * indefinitely.
     */
    void stop();

    /**
     * Get the values for the specified keys
     * @param keys list of keys to look up
     * @return future for the resulting map from key to value
     */
    Future<Map<ByteBuffer, ByteBuffer>> get(Collection<ByteBuffer> keys);

    /**
     * Set the specified keys and values.
     * @param values map from key to value
     * @param callback callback to invoke on completion
     * @return void future for the operation
     */
    Future<Void> set(Map<ByteBuffer, ByteBuffer> values, Callback<Void> callback);

    /**
     * Get all the partitions for the specified connector.
     * @param connectorName the name of the connector whose partitions are to be retrieved
     * @return set of connector partitions
     */
    Set<Map<String, Object>> connectorPartitions(String connectorName);


    /**
     * key-value的配置类，这里的Worker是Connect的配置文件中的配置
     * Configure class with the given key-value pairs
     * @param config can be DistributedConfig or StandaloneConfig
     */
    void configure(WorkerConfig config);
}
